package com.tz.dao;

import com.tz.entity.Faq;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 16-8-5.
 */
@Repository
@Transactional
public class FaqDaoImpl implements FaqDao {

    @Autowired
    private SessionFactory sessionFactory;

    private Session getSession(){
        return sessionFactory.getCurrentSession();
    }

    @Override
    public void saveFaq(Faq faq) {
        getSession().save(faq);
    }

    @Override
    public Faq getFaqById(long id) {
        String hql = "from Faq f where f.id=:id";
        return (Faq) getSession().createQuery(hql).setParameter("id",id).uniqueResult();
    }

    @Override
    public List<Faq> getAllFaqs() {
        List<Faq> faqList = new ArrayList<>();
        String hql = "from Faq";
        faqList = getSession().createQuery(hql).list();
        return faqList;
    }
}
